package com.a16.yuejian.yuejian.model;

import com.a16.yuejian.yuejian.javabean.Question;
import com.a16.yuejian.yuejian.javabean.Success;
import com.a16.yuejian.yuejian.listener.Listener;
import com.a16.yuejian.yuejian.listener.SuccessListener;
import com.a16.yuejian.yuejian.service.QuestionService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 55080 on 2017/10/17.
 */

public class QuestionModel {
    private Retrofit retrofit;
    public QuestionModel(){
        retrofit =new Retrofit.Builder()
                .baseUrl("http://10.0.2.2:8080/neu_yuejian201/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void allquestion(String coachid, final Listener listener){
        QuestionService questionService = retrofit.create(QuestionService.class);
        Call call = questionService.allquestion(coachid);
        Callback<List<Question>> callback = new Callback<List<Question>>() {
            @Override
            public void onResponse(Call<List<Question>> call, Response<List<Question>> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<List<Question>> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void ask(String cid, String uid, String qt, final SuccessListener listener){
        QuestionService questionService = retrofit.create(QuestionService.class);
        Call call = questionService.ask(cid,uid,qt);
        Callback<Success> callback = new Callback<Success>() {
            @Override
            public void onResponse(Call<Success> call, Response<Success> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<Success> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }
}
